// 以下为官方示例
$().ready(function() {
	validateRule();
	layui.use(['form','table'], function() {
		var form = layui.form,table = layui.table;
		form.on('select(pianquId)', function (data) {
			//alert(data.value);
			console.log('pro',data.value);
			selectPianqu(data.value,form)
		});
		form.on('select(pianquUserId)', function (data) {
			//alert(data.value);
			console.log('pro',data.value);
			selectPianquUser(data.value,form)
		});


		form.on('select(townId)', function (data) {
			console.log('townId',data.value);
			getVillages(data.value,form)
		});

	});
});


function getVillages(townId,form) {
	$.ajax({
		url : ctx + "module/citizen/villages",
		type : "post",
		data:{
			townId:townId
		},
		success : function(r) {
			console.log('r',r)
			if (r.code==0) {
				console.log("villages",r.villages);

				var villages = r.villages;
				if (villages.length > 0) {
					$('#villageId').empty();
					$('#villageId').append(new Option('请选择村居', ''));
					for (var i = 0; i < villages.length; i++) {
						$('#villageId').append(new Option(villages[i].townName, villages[i].id));
					}
				} else {
					$('#villageId').empty();
					$('#villageId').append(new Option('暂无村居', ''));
				}
				form.render("select");
			}
		}
	});
}

$.validator.setDefaults({
	submitHandler : function() {
		update();
	}
});
function update() {
	// var roleId = $("#roleId").val();
	// console.log('roleId',roleId)
	// console.log('data',$('#signupForm').serialize())
	$.ajax({
		cache : true,
		type : "POST",
		url : ctx +"sys/user/" + ($('#signupForm').find("#userId").val() == '' ? "save" : "update"),
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg(data.msg);
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);

			} else {
				parent.layer.msg(data.msg);
			}

		}
	});
}

function getShopByType() {
	var roleId = parseInt($("#roleId").val());
	$("#divShopId").addClass("layui-hide");
	// 商家类型  0 - 普通的汽修店商家  1 - 大型商家（比如滴滴）  2 - 保险公司   3  - 检验中心
	console.log('roleId',roleId);
	if(roleId == 4){// 审车人员
		$("#divShopId").removeClass("layui-hide");
	}else {
		$("#divShopId").addClass("layui-hide");
	}
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			name : {
				required : true
			},
			username : {
				required : true,
				minlength : 2
			},
			password : {
				required : true,
				minlength : 6
			},
			confirm_password : {
				required : true,
				minlength : 6,
				equalTo : "#password"
			},
			mobile : {
				required : true,
			},
			groupId: {
				required : true
			},
			jituanId: {
				required : true
			},
			pianquId: {
				required : true
			},
			shopId: {
				required : true
			},
		},
		messages : {
			name : {
				required : icon + "请输入姓名"
			},
			username : {
				required : icon + "请输入您的用户名",
				minlength : icon + "用户名必须两个字符以上"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码必须6个字符以上"
			},
			confirm_password : {
				required : icon + "请再次输入密码",
				minlength : icon + "密码必须6个字符以上",
				equalTo : icon + "两次输入的密码不一致"
			},
			mobile : icon + "请输入您的手机号",
			groupId : {
				required : icon + "请选择所属集团"
			},
			jituanId : {
				required : icon + "请选择集团管理员"
			},
			pianquId : {
				required : icon + "请选择片区管理员"
			},
			shopId : {
				required : icon + "请选择绑定企业"
			},
		}
	})
}

